<template>
	<view class="wrap">
		<uni-collapse :accordion="true">
			<uni-collapse-item  v-for="(item,index) in course.chapter" :title="item.name" :show-animation="true">
				<uni-list>
					<uni-list-item  :title="it.name" v-for="(it,i) in item.children" 
					 :note="'进度'+(it.rec.progress*100)+'%'" showArrow rightText="详情" />
				</uni-list>
			</uni-collapse-item>
		</uni-collapse>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				course: {},
				mustlook:[],
			}
		},
		onLoad(event) {
			console.log(event)
			uni.request({
				url:"https://api.jinkex.com/edu/v1/classroom/"+event.course_id+"/"+
				event.open_id+"?token="+uni.getStorageSync("stutoken")+
				"&school_host=haut.hnscen.cn",
				success: (res) => {
					console.log(res.data.data)
					this.course=res.data.data
					res.data.data.chapter.forEach(list=>{
						list.children.forEach(video=>{
							if(video.rec.progress!="1.000"&&video.videoid){
								this.mustlook.push({
									id:video.id,
									course_id:event.course_id,
									open_id:event.open_id
								})
							}
						})
					})
				}
			})
		},
		methods: {
			previewImage: function(e) {
				
				//获取视频地址 不知道有用没
				
			},
			go_back() {
				uni.navigateBack({});
			},
			copy() {
				uni.setClipboardData({
				    data: this.image_url,
				    success: function () {
				        uni.showToast({
				        	icon: 'none',
				        	title: '复制成功',
				        })
				    }
				});
			},
			download() {
				if (!this.image_url) {
					uni.showToast({
						icon: 'none',
						title: '没有可下载的图片!'
					})
					return;
				}
				// #ifdef MP-WEIXIN
				// 获取用户的当前设置
				uni.getSetting({
				   success(res) {
					   if(!res.authSetting.scope.writePhotosAlbum) {
						    uni.openSetting({
								success(rs) {
									console.log(rs.authSetting)
									//res.authSetting = {
										//"scope.userInfo": true,
										//"scope.userLocation": true
									//}
								}
						    });
					   }
				   }
				})
				// #endif
				const downloadTask = uni.downloadFile({
					url: this.image_url,
					success: (e) => {
						uni.saveImageToPhotosAlbum({
							filePath: e.tempFilePath,
							success: () => {
								uni.showToast({
									icon: 'none',
									title: '已保存到手机相册'
								})
							},
							fail: () => {
								uni.showToast({
									icon: 'none',
									title: '保存到手机相册失败' + e.tempFilePath
								})
							}
						});
					},
					fail: (e) => {
						uni.showModal({
							content: '下载失败，' + e.errMsg,
							showCancel: false
						})
					}
				});
				downloadTask.onProgressUpdate((res) => {
				    /* console.log('下载进度' + res.progress);
				    console.log('已经下载的数据长度' + res.totalBytesWritten);
				    console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite); */
					
					uni.showLoading({
					    title: '下载进度' + res.progress + '%'
					});
					
				    // 测试条件，取消下载任务。
				    //if (res.progress > 50) {
				        //downloadTask.abort();
				    //}
				})
			}
		}
	}
</script>

<style>
	.video-box {
	    display: block;
	    width: 660rpx;
	    height: 800rpx;
	    margin: 40rpx auto;
	}
	
	.parsing {
	    overflow: hidden;
	    display: block;
	    width: 664rpx;
	    height: 88rpx;
	    line-height: 88rpx;
	    color: #fff;
	    background-color: #337AFF;
	    box-shadow: 0 14rpx 28rpx rgba(51,122,255,.3);
	}
	
	.go-back {
	    color: #000;
	    background-color: #F8F8F8;
	    border: 1px solid rgba(5,5,5,.1);
	    box-shadow: none;
	    margin-top: 40rpx;
	}
</style>